/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package org.ipti.br.tag.bean;

import org.ipti.br.tag.bo.DisciplineBO;
import org.ipti.br.tag.bo.OrganizationBO;
import org.ipti.br.tag.bo.PlanBO;
import org.ipti.br.tag.bo.SeasonBO;
import org.ipti.br.tag.entity.Discipline;
import org.ipti.br.tag.entity.Organization;
import org.ipti.br.tag.entity.Plan;
import org.ipti.br.tag.entity.Season;
import java.util.ArrayList;
import java.util.Calendar;
import java.util.List;
import javax.inject.Inject;
import javax.faces.bean.ManagedBean;
import javax.faces.bean.ViewScoped;
import javax.faces.model.SelectItem;

/**
 *
 * @author israel
 */
@ManagedBean(name="editPlan")
@ViewScoped
public class EditPlanBean extends AppBaseBean{
    private Long planID;
    private Long disciplineID;
    private Long seasonID;
    private Plan plan;
    private boolean success;
    private List<SelectItem> disciplineList;
    private List<SelectItem> seasonList;
    private List<SelectItem> yearList;
    @Inject
    private OrganizationBO organizationBO;
    @Inject
    private DisciplineBO disciplineBO;
    @Inject
    private SeasonBO seasonBO;    
    @Inject
    private PlanBO planBO;
    
    private final int INITIAL_YEAR = 1986;
    
    public EditPlanBean() {
        
    }

    public void init() {
        plan = planBO.find(planID);
        disciplineID = plan.getDiscipline().getId();
        seasonID = plan.getSeason().getId();
        success = false;
    }
    
    public void edit() {
        Discipline discipline = new Discipline();
        discipline.setId(disciplineID);
        Season season = new Season();
        season.setId(seasonID);
        
        plan.setDiscipline(discipline);
        plan.setSeason(season);
        planBO.edit(plan);
        success = true;
        System.out.println("SUCCESS"+success);
    }
    
    public void redirectHome() {
        redirect("/educational/home/home.xhtml");
    }
    public void loadDisciplineList() {
        disciplineList = new ArrayList<SelectItem>();
        for(Discipline discipline : disciplineBO.findAll()) {
            disciplineList.add(new SelectItem(discipline.getId(), discipline.getName()));
        }
    }
    
    public void loadSeasonList() {
        seasonList = new ArrayList<SelectItem>();
        for(Season season : seasonBO.findAll()) {
            seasonList.add(new SelectItem(season.getId(), season.getDescription()));
        }
    }
    public void loadYearList() {
        yearList = new ArrayList<SelectItem>();
        for(int i = INITIAL_YEAR; i <= Calendar.getInstance().get(Calendar.YEAR); i++) {
            yearList.add(new SelectItem(i, String.valueOf(i)));
        }
    }
    
    public Plan getPlan() {
        return plan;
    }

    public void setPlan(Plan plan) {
        this.plan = plan;
    }

    public PlanBO getPlanBO() {
        return planBO;
    }

    public void setPlanBO(PlanBO planBO) {
        this.planBO = planBO;
    }

    public Long getPlanID() {
        return planID;
    }

    public void setPlanID(Long planID) {
        this.planID = planID;
    }

    public Long getDisciplineID() {
        return disciplineID;
    }

    public void setDisciplineID(Long disciplineID) {
        this.disciplineID = disciplineID;
    }

    public List<SelectItem> getDisciplineList() {
        if(disciplineList == null) {
            loadDisciplineList();
        }
        return disciplineList;
    }

    public void setDisciplineList(List<SelectItem> disciplineList) {
        this.disciplineList = disciplineList;
    }

    public Long getSeasonID() {
        return seasonID;
    }

    public void setSeasonID(Long seasonID) {
        this.seasonID = seasonID;
    }

    public List<SelectItem> getSeasonList() {
        if(seasonList == null) {
            loadSeasonList();
        }
        return seasonList;
    }

    public void setSeasonList(List<SelectItem> seasonList) {
        this.seasonList = seasonList;
    }

    public List<SelectItem> getYearList() {
        if(yearList == null) {
            loadYearList();
        }
        return yearList;
    }

    public void setYearList(List<SelectItem> yearList) {
        this.yearList = yearList;
    }

    public DisciplineBO getDisciplineBO() {
        return disciplineBO;
    }

    public void setDisciplineBO(DisciplineBO disciplineBO) {
        this.disciplineBO = disciplineBO;
    }

    public OrganizationBO getOrganizationBO() {
        return organizationBO;
    }

    public void setOrganizationBO(OrganizationBO organizationBO) {
        this.organizationBO = organizationBO;
    }

    public SeasonBO getSeasonBO() {
        return seasonBO;
    }

    public void setSeasonBO(SeasonBO seasonBO) {
        this.seasonBO = seasonBO;
    }

    public boolean isSuccess() {
        return success;
    }

    public void setSuccess(boolean success) {
        this.success = success;
    }
    
}
